package com.daxt.service.impl;

import com.daxt.common.util.DateUtil;
import com.daxt.mapper.archives.ArchivesBehaveLogMapper;
import com.daxt.mapper.sys.DeptMapper;
import com.daxt.mapper.sys.UserMapper;
import com.daxt.model.service.archives.vo.ArchivesBehaveLog;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.base.vo.BaseUser;
import com.daxt.model.service.base.vo.Dept;
import com.daxt.service.ArchivesBehaveLogService;
import com.daxt.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArchivesBehaveLogServiceImpl implements ArchivesBehaveLogService {

    @Autowired
    private ArchivesBehaveLogMapper archivesBehaveLogMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void saveArchivesBehaveLog(ArchivesBehaveLog archivesBehaveLog, UserInfo loginUser , HttpServletRequest request) {
        String ip= IpUtil.getIP(request);

        BaseUser baseUsere = userMapper.selectById(loginUser.getId());
        List<Dept> deptList2=new ArrayList<>();
        if(baseUsere.getDeptIds()!=null) deptList2 = deptMapper.getAllParentDept(baseUsere.getDeptIds());
        String dept="";
        if(deptList2.size()==2){
            dept=deptList2.get(1).getDeptName();
        }else if(deptList2.size() > 2){
            dept=deptList2.get(1).getDeptName()+" "+deptList2.get(deptList2.size()-1).getDeptName();
        }


        archivesBehaveLog.setOperationById(loginUser.getId().toString());
        archivesBehaveLog.setOperationBy(loginUser.getRealName());
        archivesBehaveLog.setOperationLoginName(loginUser.getUsername());
        archivesBehaveLog.setOperationByDept(dept);
        if("admin".equals(loginUser.getRole())){
            archivesBehaveLog.setOperationByRole("超级管理员");
        }else{
            archivesBehaveLog.setOperationByRole(loginUser.getRole());
        }

        archivesBehaveLog.setFondsId(loginUser.getFondsId());
        archivesBehaveLog.setOperationByIp(ip);
        archivesBehaveLog.setCreateTime(DateUtil.getCurrentTime());

        archivesBehaveLogMapper.insert(archivesBehaveLog);


    }
}
